因為颱風天大雨沒有借到可以執行的電腦用,有點可惜。
這次介紹的是寫出csv檔案,最簡單的寫法如下:
yourname = paste(Sys.getenv("USERNAME")) #自動找出本機使用者名稱
write.csv(data,paste("c:\\Users\\",yourname,"\\Downloads\\abc.csv",sep=""))
會有兩個問題,這樣寫入的CSV檔案會出現左邊一行流水碼,因此我們會用
write.csv(data,paste("c:\\Users\\",yourname,"\\Downloads\\abc.csv",sep=""),row.names=FALSE, na="")
最後是說,如果你不想要覆蓋掉你每一次讀出的檔案,我選擇自己寫一個function:
practice = data.frame("txkey" = 1:5,"fraud_ind" = 6:10)
write_csv = function(names = gsub("-","",Sys.Date()),practice,path = paste("c:\\Users\\",yourname,"\\Downloads\\practice\\",sep="")){
yourname = paste(Sys.getenv("USERNAME")) #自動找出本機使用者名稱
xls=list.files(path,pattern = names)
if(length(xls)<1){
write.csv(practice,paste("c:\\Users\\",yourname,"\\Downloads\\practice\\",names,".csv",sep=""))
}else{
write.csv(practice,paste("c:\\Users\\",yourname,"\\Downloads\\practice\\",names,"_",length(xls),".csv",sep=""))
}
}
write_csv(practice = practice)